package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpSellerStoreProductInfringingKeywordsRecord;
import com.erp.gather.domain.bo.ErpSellerStoreProductInfringingKeywordsRecordBo;
import com.erp.gather.domain.vo.ErpSellerStoreProductInfringingKeywordsRecordVo;
import com.erp.gather.mapper.ErpSellerStoreProductInfringingKeywordsRecordMapper;
import com.erp.gather.service.IErpSellerStoreProductInfringingKeywordsRecordService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商品侵权词Service业务层处理
 *
 * @author erp
 * @date 2023-12-11
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreProductInfringingKeywordsRecordServiceImpl implements IErpSellerStoreProductInfringingKeywordsRecordService {

    private final ErpSellerStoreProductInfringingKeywordsRecordMapper baseMapper;

    /**
     * 查询商品侵权词
     */
    @Override
    public ErpSellerStoreProductInfringingKeywordsRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品侵权词列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreProductInfringingKeywordsRecordVo> queryPageList(ErpSellerStoreProductInfringingKeywordsRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreProductInfringingKeywordsRecord> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreProductInfringingKeywordsRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品侵权词列表
     */
    @Override
    public List<ErpSellerStoreProductInfringingKeywordsRecordVo> queryList(ErpSellerStoreProductInfringingKeywordsRecordBo bo) {
        LambdaQueryWrapper<ErpSellerStoreProductInfringingKeywordsRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreProductInfringingKeywordsRecord> buildQueryWrapper(ErpSellerStoreProductInfringingKeywordsRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreProductInfringingKeywordsRecord> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getRecordName()), ErpSellerStoreProductInfringingKeywordsRecord::getRecordName, bo.getRecordName());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordUrl()), ErpSellerStoreProductInfringingKeywordsRecord::getRecordUrl, bo.getRecordUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ErpSellerStoreProductInfringingKeywordsRecord::getStatus, bo.getStatus());
        lqw.eq(bo.getType() != null, ErpSellerStoreProductInfringingKeywordsRecord::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreProductInfringingKeywordsRecord::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增商品侵权词
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreProductInfringingKeywordsRecordBo bo) {
        ErpSellerStoreProductInfringingKeywordsRecord add = BeanUtil.toBean(bo, ErpSellerStoreProductInfringingKeywordsRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品侵权词
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreProductInfringingKeywordsRecordBo bo) {
        ErpSellerStoreProductInfringingKeywordsRecord update = BeanUtil.toBean(bo, ErpSellerStoreProductInfringingKeywordsRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreProductInfringingKeywordsRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品侵权词
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
